from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys

def waitxpath(driver, xpath):
    try:
        # 显示等待20秒
        WebDriverWait(driver, 20, 1).until(
            lambda x: x.find_element_by_xpath(xpath)
        )
    except Exception as e:
        raise e

chromedriver = 'chromedriver'
driver = webdriver.Chrome(executable_path=chromedriver)
driver.get('https://www.google.com')
# 搜索框元素对应的XPath
search_input_xpath = '//*[@id="tsf"]/div[2]/div[1]/div[1]/div/div[2]/input'
# 显示等待
waitxpath(driver, search_input_xpath)
# 通过XPath定位元素
search_input = driver.find_element_by_xpath(search_input_xpath)
search_input.send_keys('python')
search_input.send_keys(Keys.ENTER)